package com.ease.database.initializer;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.ease.common.util.Detect;
import com.ease.common.util.FileUtil;
import com.ease.common.util.IpAddressUtil;
import com.ease.common.vo.MapParameter;
import com.ease.dao.JdbcTemplateDataAccessObject;
import com.obas.test.ApplicationContextUserContextTest;

/**
 * @author nathanleewei
 * 

 */

//select t.userip || '_' || t.userbeginport || '_' || t.userendport,
//bs.name,
//ba.name,
//t.*
//from i_dpi_kpi t, m_businessapplication ba, m_businessserving bs
//where t.serverid = bs.identity
//and t.appid = ba.identity
//and t.batchtimestamp =
//TO_DATE('2013-11-11 12:12:12', 'YYYY-MM-DD HH24:MI:SS')
//order by t.userip, t.userbeginport, t.userendport, t.serverid, t.appid
public class DpiIpAddressInitializer extends ApplicationContextUserContextTest {

	@Autowired
	JdbcTemplateDataAccessObject jdbcTemplateDataAccessObject;

	@Test
	public void test() {
		test("USERIP");
		test("DOMAINIP");
		test("SERVERIP");
	}

	public void test(String columnName) {
		int i = 0;
		while (true) {
			i++;
			List<Map<String, Object>> datas = jdbcTemplateDataAccessObject.find("SELECT " + columnName + "  FROM I_DPI_KPI WHERE INSTR(" + columnName + ",'.',1,1)>0  ");
			if (notEmpty(datas)) {
				for (Map<String, Object> data : datas) {
					String value = String.valueOf(data.get(columnName));
					if (value.indexOf(".") > 0) {

					} else {
						MapParameter parameter = new MapParameter();
						parameter.put("rawa", value);
						parameter.put("newa", IpAddressUtil.fromDecimal(Long.parseLong(value)));

						jdbcTemplateDataAccessObject.update("UPDATE I_DPI_KPI SET " + columnName + " = :newa WHERE " + columnName + "=:rawa", parameter);
					}

				}

			} else {
				break;
			}
			if (i > 5000) {
				break;
			}
		}
	}

}
